package com.zhuangfei.hpuhelper.view;

import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.hpu.bean.Lecture;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.BodyScoreParser;
import com.hpu.parser.LectureParser;
import com.hpu.service.BodyScoreService;
import com.hpu.service.LectureService;
import com.hpu.service.LoginService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.toolkit.IntentToolkit;
import com.zhuangfei.toolkit.tools.BundleTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lzf.serviceplatform.R;

public class LectureActivity extends BaseActivity {

    List<Map<String, String>> items = new ArrayList<>();
    SimpleAdapter adapter;

    @BindView(R.id.id_lecture_listview)
    public ListView mListView;

    private LectureService lectureService;
    private LoginService loginService;

    Class returnClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lecture);
        ActivityTools.setTransparent(this);
        ButterKnife.bind(this);
        initView();
        lectureService.getLecture();
    }

    private void initView() {// 业务逻辑对象
        returnClass= BundleTools.getFromClass(this,MenuActivity.class);

        lectureService = new LectureService(handler, this);
        loginService = new LoginService(this,handler);


        adapter=new SimpleAdapter(this,items,R.layout.item_lecture_layout,
                new String[]{"name","content","who","time","place"},
                new int[]{R.id.id_lecture_name,R.id.id_lecture_content,R.id.id_lecture_who,R.id.id_lecture_time,R.id.id_lecture_place});
        mListView.setAdapter(adapter);
    }

    DefaultHandler handler = new DefaultHandler(MyApplication.getInstance()) {
        public void handleMessage(Message msg) {
            getViewHelper().showSuccessView();
            super.handleMessage(msg);
            switch (msg.what) {
                case HttpConstants.QUERY_LECTURE_SUCCESS:
                    Log.i("Lecture",msg.obj.toString());
                    LectureParser lectureParser=new LectureParser();
                    List<Lecture> list=lectureParser.parse(msg.obj.toString());
                    showResult(list);
                    break;

                // VPN过期，重新登录
                case HttpConstants.VPN_TOOLONG:
                    if(handler.isIgnoreVpnExpired()){
                        break;
                    }

                    getViewHelper().showLoadingView();
                    loginService.login(ShareTools.get(LectureActivity.this, ShareConstants.STORE_USER, "").toString(),
                            ShareTools.get(LectureActivity.this,ShareConstants.STORE_PASSWORD1, "").toString(),
                            ShareTools.get(LectureActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
                    break;

                // 登录成功,重新获取
                case HttpConstants.LOGIN_SUCCESS:
                    getViewHelper().showLoadingView();
                    ShareTools.put(LectureActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
                    lectureService.getLecture();
                    break;
            }
        };
    };

    protected void showResult(List<Lecture> list) {
        if(list==null) return;
        items.clear();

        if(list.size()==0){
            getViewHelper().showEmptyView("没有讲座信息");
        }
        for(int i=0;i<list.size();i++){
            Lecture lecture=list.get(i);
            Map<String,String> map=new HashMap<>();
            map.put("name",lecture.getName());
            map.put("who","主讲人:"+lecture.getWho());
            map.put("time","时间:"+lecture.getTime());
            map.put("place","地点:"+lecture.getPlace());
            map.put("content",lecture.getContent());
            items.add(map);
        }

        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.id_back)
    public void goBack(){
        new IntentToolkit().with(this)
                .target(returnClass)
                .canBack()
                .execute();
    }

    @Override
    public void onBackPressed() {
        goBack();
    }
}
